#include <iostream>
#include <termios.h>
#include <fcntl.h>

using namespace std;

int main1(int argc, char* argv[])
{
	char writeBuffer[5] = {1, 255, 25, 51, 127};
	char readBuffer[1024];
	int count;
	struct termios options1, options2;
	int fd1, fd2;

	cout << "Opening serial port 1" << endl;
	fd1=open("/dev/ttySAC1", O_RDWR | O_NOCTTY | O_NDELAY);
	tcgetattr(fd1, &options1);
	cfsetispeed(&options1, B115200);
	cfsetospeed(&options1, B115200);
	options1.c_cflag = (options1.c_cflag & ~CSIZE) | CS8;
	options1.c_iflag = IGNBRK;
	options1.c_lflag = 0;
	options1.c_oflag = 0;
	options1.c_cflag |= CLOCAL | CREAD;
	options1.c_cc[VMIN] = 1;
	options1.c_cc[VTIME] = 5;
	options1.c_iflag &= ~(IXON|IXOFF|IXANY);
	options1.c_cflag &= ~(PARENB | PARODD);
	tcsetattr(fd1,TCSANOW, &options1);

	cout << "Opening serial port 2" << endl;
	fd2=open("/dev/ttySAC2", O_RDWR | O_NOCTTY | O_NDELAY);
	tcgetattr(fd2, &options2);
	cfsetispeed(&options2, B115200);
	cfsetospeed(&options2, B115200);
	options2.c_cflag = (options2.c_cflag & ~CSIZE) | CS8;
	options2.c_iflag = IGNBRK;
	options2.c_lflag = 0;
	options2.c_oflag = 0;
	options2.c_cflag |= CLOCAL | CREAD;
	options2.c_cc[VMIN] = 1;
	options2.c_cc[VTIME] = 5;
	options2.c_iflag &= ~(IXON|IXOFF|IXANY);
	options2.c_cflag &= ~(PARENB | PARODD);
	tcsetattr(fd2,TCSANOW, &options2);

	cout << "Sending 5 bytes on serial port 1" << endl;
	write(fd1, (char*)writeBuffer, 5);
	sleep(5);

	cout << "Receiving data on serial port 2 " << endl;
	count = read(fd2, readBuffer, 1024);

	cout << "Bytes received: " << count << endl;

	cout << "Data: " << endl;

	for(int i = 0; i < count; i++)
	{
		cout << (int)readBuffer[i] << endl;
	}

	close(fd1);
	close(fd2);

	return 0;
}
